package com.example.stumanager.student;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.stumanager.R;
import com.example.stumanager.database.LateReturnSql;
import com.example.stumanager.entity.LateReturnEntity;

import java.io.ByteArrayOutputStream;
/**
 * 晚归打卡页面操作
 */
public class StudentLateReturnPage extends AppCompatActivity {
    EditText name, number, explains;
    ImageView im1;
    Button upload1, confirm,re;
    RadioButton state;
    int i1 = 0;
    String face;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.student_late_return_page);
        im1 = findViewById(R.id.im1);
        state = findViewById(R.id.state);
        upload1 = findViewById(R.id.im11);
        explains = findViewById(R.id.explains);
        name = findViewById(R.id.name);
        number = findViewById(R.id.number);

        state.setChecked(true);
        re = findViewById(R.id.re);
        re.setOnClickListener(view -> finish());
        confirm = findViewById(R.id.daka);
        upload1.setOnClickListener(view -> {
            Intent intent = new Intent(Intent.ACTION_PICK, null);
            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    "image/*");
            startActivityForResult(intent, 1);
        });

        confirm.setOnClickListener(view -> {
            Bitmap bitmap = ((BitmapDrawable) im1.getDrawable()).getBitmap();
            String s = bitmapToString(bitmap);
            if (s.isEmpty()) {
                face="";
            } else {
                face=s;
            }

            if (name.getText().toString().isEmpty() || number.getText().toString().isEmpty()
                    || explains.getText().toString().isEmpty()) {
                Toast.makeText(StudentLateReturnPage.this, "信息需填写完整！", Toast.LENGTH_SHORT).show();
                return;
            }
            boolean checked = state.isChecked();
            if (checked) {
                state.setText("已报备");
            } else {
                state.setText("未报备");
            }


            LateReturnEntity f = new LateReturnEntity(number.getText().toString(),
                    name.getText().toString(),
                    explains.getText().toString(),
                    state.getText().toString(),
                    face
            );

            LateReturnSql db = new LateReturnSql(getBaseContext());
            if (db.add(f)) {
                Toast.makeText(StudentLateReturnPage.this, "打卡成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(StudentLateReturnPage.this, "打卡失败", Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            Uri u = data.getData();
            ContentResolver cr = StudentLateReturnPage.this.getContentResolver();
            try {
                Bitmap bit;
                bit = BitmapFactory.decodeStream(cr.openInputStream(u));
                BitmapDrawable bd = new BitmapDrawable(bit);
                im1.setImageDrawable(bd);
                i1 = 1;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    //将图片转换成String（ byte[] ）
    public static String bitmapToString(Bitmap bitmap) {
        //将Bitmap转换成字符串
        String string = null;
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
        byte[] bytes = bStream.toByteArray();
        string = Base64.encodeToString(bytes, Base64.DEFAULT);
        return string;


    }




}